package com.gzwanchuang.lszg.dialog;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gzwanchuang.lszg.R;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by FrankZhang on 2017/11/16.
 */

public class DialogUtils {

    public static void showDefaultDialog(Context context, String title, String content, final DialogImpl listener) {
        showDefaultDialog(context, title, content, "确定", "取消", listener);
    }

    public static void showDefaultDialog(Context context, String title, String content, String ok, String cancel, final DialogImpl listener) {
        androidx.appcompat.app.AlertDialog.Builder builder = new androidx.appcompat.app.AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(content);
        builder.setNegativeButton(cancel, (dialog, which) -> listener.onCancel());
        builder.setPositiveButton(ok, (dialog, which) -> listener.onOk());
        builder.show();
    }

    public static Dialog showHitDialog(Context context, String title, String content) {
        return showHitDialog(context, title, content, null);
    }

    public static Dialog showHitDialog(Context context, String title, String content, DialogImpl impl) {
        androidx.appcompat.app.AlertDialog.Builder builder = new androidx.appcompat.app.AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(content);
//        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", (dialog, which) -> {
            if (impl != null) {
                impl.onOk();
            }
        });
        return builder.show();
    }

    public static class DialogImpl {
        public void onOk() {
        }

        public void onCancel() {
        }
    }


    /**
     * 底部菜单选择
     *
     * @param context
     * @param menu1_str
     * @param menu2_str
     * @param impl
     */
    public static void showBottomMenuDialog(Context context, String title, String menu1_str, String menu2_str, BottomMenuDialogImpl impl) {
        final Dialog mBottomSheetDialog = new Dialog(context, R.style.MaterialDialogSheet);
        View view = View.inflate(context, R.layout.dialog_bottom_tow_menu_sheet, null);
        TextView title_tv = view.findViewById(R.id.title_tv);
        title_tv.setText(title);
        TextView menu1 = view.findViewById(R.id.menu1);
        TextView menu2 = view.findViewById(R.id.menu2);
        menu1.setText(menu1_str);
        menu2.setText(menu2_str);
        TextView cancel = view.findViewById(R.id.cancel);

        menu1.setOnClickListener(v -> {
            mBottomSheetDialog.dismiss();
            impl.onMenu1();
        });
        menu2.setOnClickListener(v -> {
            mBottomSheetDialog.dismiss();
            impl.onMenu2();
        });

        cancel.setOnClickListener(v -> mBottomSheetDialog.dismiss());

        mBottomSheetDialog.setContentView(view);
        mBottomSheetDialog.setCancelable(true);
        mBottomSheetDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mBottomSheetDialog.getWindow().setGravity(Gravity.BOTTOM);
        mBottomSheetDialog.show();
    }

    public interface BottomMenuDialogImpl {
        void onMenu1();

        void onMenu2();
    }


    public static void showDatePickerDialog(Context activity, DatePickerDialog.OnDateSetListener listener, Calendar calendar) {
        // Calendar 需要这样来得到
        // Calendar calendar = Calendar.getInstance();
        // 直接创建一个DatePickerDialog对话框实例，并将它显示出来
        DatePickerDialog datePickerDialog = new DatePickerDialog(activity,
                // 绑定监听器(How the parent is notified that the date is set.)
                listener
                // 设置初始日期
                , calendar.get(Calendar.YEAR)
                , calendar.get(Calendar.MONTH)
                , calendar.get(Calendar.DAY_OF_MONTH));
        Date date = new Date();//当前时间
        long time = date.getTime();
        DatePicker datePicker = datePickerDialog.getDatePicker();
        datePicker.setMaxDate(time);//设置最大日期
        datePickerDialog.show();
    }



//    private void startDownAnimation(View view) {
//        Animation slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
//                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
//                0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
//
//        slide.setDuration(400);
//        slide.setFillAfter(true);
//        slide.setFillEnabled(true);
//        slide.setAnimationListener(new Animation.AnimationListener() {
//            @Override
//            public void onAnimationStart(Animation animation) {
//
//            }
//
//            @Override
//            public void onAnimationEnd(Animation animation) {
//                dismiss();
//            }
//
//            @Override
//            public void onAnimationRepeat(Animation animation) {
//
//            }
//        });
//        view.startAnimation(slide);
//    }

}
